package com.qst.crop.demo.controller;

import com.qst.crop.demo.common.Result;
import com.qst.crop.demo.entity.Answer;
import com.qst.crop.demo.entity.Question;
import com.qst.crop.demo.entity.User;
import com.qst.crop.demo.service.AnsService;
import com.qst.crop.demo.service.QuesService;
import com.qst.crop.demo.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.sql.Date;
import java.util.List;

@RestController
@RequestMapping("/ans")
@CrossOrigin(origins = "*")
public class AnsController {

    @Autowired
    AnsService ansService;
    @Autowired
    UserService userService;
    @Autowired
    QuesService quesService;

    /**
     *  添加回答
     * @param email
     * @param answerText
     * @param questionId
     * @return
     */
    @RequestMapping("/add")
    public Result add(String email, String answerText, Long questionId){
        System.out.println("添加回答");

        if(!StringUtils.hasLength(email) || !StringUtils.hasLength(answerText) || questionId <= 0){
            return Result.fail(-1, "无效参数");
        }

        User userinfo = userService.getUserByEmail(email);
        if(userinfo == null){
            return Result.fail(-2, "不存在该用户");
        }

        if(quesService.getQuesById(questionId) == null){
            return Result.fail(-3, "不存在该问题");
        }

        if(ansService.add(email, answerText, questionId, new Date(System.currentTimeMillis())) == 1){

            return Result.success(200, "添加成功");
        }else{

            return Result.fail(-3, "添加失败");
        }
    }


    /**
     *  删除回答
     * @param email
     * @param answerId
     * @return
     */
    @RequestMapping("/del")
    public Result delete(String email, Long answerId){
        System.out.println("删除回答");

        if(!StringUtils.hasLength(email) || answerId <= 0){
            return Result.fail(-1, "无效参数");
        }

        User user = userService.getUserByEmail(email);
        if(user == null){
            return Result.fail(-2, "用户不存在");
        }

        if(ansService.deleteById(answerId) == 1){

            return Result.success(200, "删除成功");
        }else{

            return Result.fail(-3, "回答不存在");
        }
    }

    /**
     *  根据id获取回答
     * @param answerId
     * @return
     */
    @RequestMapping("/getAnsById")
    public Result getQuesById(Long answerId){
        System.out.println("通过id获取回答");

        if(answerId <= 0){
            return Result.fail(-1, "无效参数");
        }

        Answer answer = ansService.getAnsById(answerId);
        if(answer == null){

            return Result.fail(-2, "回答不存在");
        }else{

            return Result.success(200, answer, "查找成功");
        }
    }

    /**
     *  搜索回答
     * @param searchWord
     * @return
     */
    @RequestMapping("/search")
    public Result select(String searchWord){
        System.out.println("搜索回答");
        if(searchWord == null){
            return Result.fail(-1, "非法请求");
        }

        List<Answer> answers = ansService.select(searchWord);
        if(answers.size() == 0){
            return Result.fail(-2, "未找到相关问题");
        }

        return Result.success(200, answers);
    }

    /**
     *  通过问题id获得回答列表
     * @param questionId
     * @return
     */
    @RequestMapping("/getAnsList")
    public Result getAnsList(Long questionId){
        System.out.println("通过问题id获得回答");

        if(questionId <= 0){
            return Result.fail(-1, "非法参数");
        }

        List<Answer> answers = ansService.getAnsList(questionId);
        if(answers == null || answers.size() == 0){
            return Result.fail(-2, "回答不存在");
        }

        return Result.success(200, answers);
    }

}
